﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Face;
using JYStockAnalyser.Model;
using JYStockAnalyser.ClientLib;

namespace JYStockAnalyser.WinUI
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //System.Runtime.Remoting.RemotingServices.
            //if (this.comboBox1.SelectedIndex == 0)
            //{
            //    System.Runtime.Remoting.RemotingConfiguration.Configure("dianxin.config");
            //}
            //else if (this.comboBox1.SelectedIndex == 1)
            //{
            //    System.Runtime.Remoting.RemotingConfiguration.Configure("wangtong.config");
            //}
            //m_enterMainForm();

            string userName = this.txtUserName.Text.Trim();
            string password = this.txtPassword.Text.Trim();

            if (string.IsNullOrEmpty(userName))
            {
                Tool.ShowInformationBox("请输入用户名!");
                this.txtUserName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(password))
            {
                Tool.ShowInformationBox("请输入密码!");
                this.txtPassword.Focus();                
                return;
            }

            //if (userName.ToLower().Equals("guest"))
            //{
            //    m_enterGuestMainForm();
            //    return;
            //}

            User user;
            byte result = new UserManageFace().Login(userName, password, out user);

            ComputerUniqueNO uniqueNO = IDGeter.GetUniqueNO();
            switch (result)
            {
                case 1:
                    if (!uniqueNO.Equals(user.UniqueNO))
                    {
                        Tool.ShowInformationBox("用户只能在其绑定的计算机上使用!");
                        break;
                    }
                    CurrentInfo.User = user;
                    ParametersManager.SetParameterValue(ParametersManager.PARAM_USERNAME, userName);
                    m_enterMainForm();
                    break;
                case 2:
                    Tool.ShowInformationBox("没有此用户!");
                    this.txtUserName.Focus();
                    this.txtUserName.SelectAll();
                    break;
                case 3:
                    Tool.ShowInformationBox("密码错误!");
                    this.txtPassword.Focus();
                    this.txtPassword.SelectAll();
                    break;                
                case 5:
                    Tool.ShowInformationBox("用户已过期!");
                    break;
                case 6:
                    if (Tool.ShowQuestionBox("此用户为首次使用，确认后将与当前计算机绑定，绑定后此用户将不能在其它计算机上使用，确认要绑定吗？") == DialogResult.Yes)
                    {
                        if (Tool.ShowQuestionBox("绑定后将不能恢复，请再次确认!") == DialogResult.Yes)
                        {
                            if (new UserManageFace().BindComputer(userName, uniqueNO) == 1)
                            {
                                Tool.ShowInformationBox("绑定成功!");
                                CurrentInfo.User = user;
                                ParametersManager.SetParameterValue(ParametersManager.PARAM_USERNAME, userName);
                                m_enterMainForm();
                            }
                            else
                            {
                                Tool.ShowInformationBox("绑定失败!");
                            }
                        }
                    }
                    break;
            }          
        }

        /// <summary>
        /// 进入主界面
        /// </summary>
        private void m_enterMainForm()
        {           
            if (CurrentInfo.User.IsGuest)
            {
                m_enterGuestMainForm();
            }
            else
            {
                m_Logined = true;
                this.Close();
            }
            //this.Hide();
            //FrmMain.Instance.WindowState = FormWindowState.Maximized;
            //FrmMain.Instance.ShowDialog();

            //Application.Exit();
        }

        /// <summary>
        /// 进入主界面
        /// </summary>
        private void m_enterGuestMainForm()
        {
            m_Logined = true;
            this.Close();
            FrmMain.Instance.Guest();
        }


        bool m_Logined = false;
                                                                                                           
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.txtUserName.Text = ParametersManager.GetParameterValue(ParametersManager.PARAM_USERNAME);
            this.lblTitle.Text = Configer.GetApplicationName();
            this.comboBox1.SelectedIndex = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void FrmLogin_Paint(object sender, PaintEventArgs e)
        {     
           
        }

        private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!m_Logined)
            {
                Application.Exit();
            }
        }
    }
}
